package com.example.mybatis.demo.dao;

import com.example.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper // 将对象交给Spring管理
public interface UserInfoDao {
    // 查询所有用户信息
    @Select("select * from user_info")
    List<UserInfo> selectAll();

//    @Select("select * from user_info")
//    UserInfo selectAll2();

    @Select("select * from user_info where id=#{aa}")
    UserInfo selectById(Integer id);

    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, " +
            "update_time as updateTime from user_info")
    List<UserInfo> select2();

//    @Results(id="BaseMap",value={
//            @Result(column = "delete_flag",property = "deleteFlag"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
    @Select("select id, username, password, age, gender, phone,delete_flag, create_time, update_time from user_info")
    List<UserInfo> select3();

    // 非唯一主键查询,返回结果建议使用List
//    @ResultMap("BaseMap")
    @Select("select id, username, password, age, gender, phone,delete_flag, create_time, update_time from user_info where age=#{age1} and gender=#{gender1}")
    List<UserInfo> selectUserByAgeAndGender(@Param("gender1")Integer gender,@Param("age1") Integer age);

    // 传递对象
    @Insert("insert into user_info (username,`password`,age,gender) " +
            "values (#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo(UserInfo userInfo);

    //
    @Options(useGeneratedKeys = true,keyProperty="id")
    @Insert("insert into user_info (username,`password`,age,gender) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insertUserInfo2(@Param("userInfo") UserInfo userInfo);


    // 删除数据
    @Delete("delete from user_info where id=#{id}")
    Integer delete(Integer id);

    // 修改数据
    @Update("update user_info set password=#{password} where id=#{id}")
    Integer update(Integer id,String password);

    @Update("update user_info set password=#{password} where id=#{id}")
    Integer update2(UserInfo userInfo);

    //  #和$的区别
    @Select("select * from user_info where id='${id}'")
    UserInfo selectById2(Integer id);

    @Select("select * from user_info where username=#{name}")
    List<UserInfo> selectByName(String name);

    @Select("select * from user_info where username=#{name} and password='${password}'")
    List<UserInfo> selectByNameAndPassword(String name,String password);

    // 排序功能
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

//    @Select ("select * from user_info where username like '%${name}%'")
    @Select ("select * from user_info where username like concat('%',#{name},'%')")
    List<UserInfo> selectUserByLike(String name);

    @Insert("<script>" +
            "insert into user_info" +
            "            <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "                <if test=\"username!=null\">username,</if>" +
            "                <if test=\"password!=null\">`password`,</if>" +
            "                <if test=\"age!=null\">age,</if>" +
            "                <if test=\"gender!=null\">gender</if>" +
            "            </trim>" +
            "        values" +
            "            <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "                <if test=\"username!=null\">#{username},</if>" +
            "                <if test=\"password!=null\">#{password},</if>" +
            "                <if test=\"age!=null\">#{age},</if>" +
            "                <if test=\"gender!=null\">#{gender}</if>" +
            "            </trim>"+
            "</script>")
    Integer insertUserByCondition(UserInfo userInfo);

}
